package com.lv.leyou.web;

import com.lv.leyou.dto.SpecGroupDTO;
import com.lv.leyou.dto.SpecParamDTO;
import com.lv.leyou.service.SpecGroupService;
import com.lv.leyou.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    //根据分类id查询规格组
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long id) {

        return ResponseEntity.ok(this.specGroupService.querySpecGroupByCategoryId(id));
    }

    //查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(this.specParamService.querySpecParam(categoryId, groupId, searching));
    }

    //新增规格组
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(SpecGroupDTO specGroupDTO) {
        specGroupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(201).build();
    }

    //修改规格组
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(SpecGroupDTO specGroupDTO) {
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(204).build();
    }

    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(SpecParamDTO specParamDTO) {
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(201).build();
    }

    //修改规格参数
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecGroup(SpecParamDTO specParamDTO) {
        specParamService.updateSpecGroup(specParamDTO);
        return ResponseEntity.status(204).build();
    }

    //查询规格组及规格参数
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupAndSpecParamByCategoryId(@RequestParam("id") Long categoryId) {

        return ResponseEntity.ok( specGroupService.querySpecGroupAndSpecParamByCategoryId(categoryId));
    }
}
